all files / drawer/ drawer.vue

27.66% Statements 13/47
13.33% Branches 4/30
6.25% Functions 1/16
19.44% Lines 7/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109              1× 1× 1× 1× 1× 1×                                                                                                                                                                                                
//
//
//
//
//
//
 
import keycode from 'keycode'
import paper from '../paper'
import PopupManager from '../internal/popup/manager'
import {getZIndex} from '../internal/popup/utils'
import {getWidth} from '../utils'
const transitionEvents = ['msTransitionEnd', 'mozTransitionEnd', 'oTransitionEnd', 'webkitTransitionEnd', 'transitionend']
export default {
  name: 'mu-drawer',
  props: {
    right: {
      type: Boolean,
      default: false
    },
    open: {
      type: Boolean,
      default: false
    },
    docked: {
      type: Boolean,
      default: true
    },
    width: {
      type: [Number, String]
    },
    zDepth: {
      type: Number,
      default: 2
    }
  },
  data () {
    return {
      overlayZIndex: getZIndex(),
      zIndex: getZIndex()
    }
  },
  computed: {
    drawerStyle () {
      return {
        width: getWidth(this.width),
        'z-index': this.docked ? '' : this.zIndex
      }
    }
  },
  methods: {
    overlayClick () {
      this.$emit('close', 'overlay')
    },
    escPress () {
      this.$emit('close', 'keyboard')
    },
    bindTransition () {
      this.handleTransition = (e) => {
        if (e.propertyName !== 'transform') return
        if (!this.docked) this.$emit(this.open ? 'show' : 'hide')
      }
      transitionEvents.forEach((eventName) => {
        this.$el.addEventListener(eventName, this.handleTransition)
      })
    },
    unBindTransition () {
      if (!this.handleTransition) return
      transitionEvents.forEach((eventName) => {
        this.$el.removeEventListener(eventName, this.handleTransition)
      })
    },
    resetZIndex () {
      this.overlayZIndex = getZIndex()
      this.zIndex = getZIndex()
    }
  },
  watch: {
    open (val) {
      if (val && !this.docked) {
        this.resetZIndex()
        PopupManager.open(this)
      } else {
        PopupManager.close(this)
      }
    },
    docked (val, oldVal) {
      if (val && !oldVal) {
        PopupManager.close(this)
      }
    }
  },
  mounted () {
    if (this.open && !this.docked) PopupManager.open(this)
    window.addEventListener('keydown', (event) => {
      if (keycode(event) === 'esc' && !this.docked) this.escPress()
    })
    this.bindTransition()
  },
  beforeDestroy () {
    PopupManager.close(this)
    this.unBindTransition()
  },
  components: {
    paper
  }
}